package tt.dz.appserver.util.json;

import java.lang.reflect.Field;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.user.UserInfo;
import tt.dz.appserver.util.annotation.AnnotationUtils;
import tt.dz.appserver.util.encoding.EncodingUtils;
import tt.dz.appserver.util.enums.Response;

/**
 * JSON帮助类
 * 
 * @author 谢龙飞
 *
 */
@SuppressWarnings("unchecked")
public final class JsonUtils {


	/**
	 * json转对象
	 * 
	 * @param obj
	 * @param cls
	 * @return
	 */
	public final static <T> T JsonToObjectOfClass(final JSONObject jsonObject, final Class<T> cls) {

		// 获取类所有属性
		Field fields[] = cls.getDeclaredFields();
		try {
			// 创建对象
			Object obj = cls.newInstance();
			// 循环属性
			for (Field field : fields) {

				// 获取属性名称
				String fieldName = AnnotationUtils.getJsonMapping(field);
				
				// 驼峰属性
				String camelFieldName = EncodingUtils.camelName(fieldName);
				// 下划线属性
				String underscoreFieldName = EncodingUtils.underscoreName(fieldName);

				// 设置不安全检查
				field.setAccessible(true);

				Class<?> typeName = field.getType();

				// 判断是否存在此驼峰属性
				if (jsonObject.containsKey(camelFieldName)) {

					// 判断类型并调用set方法
					if (typeName.equals(Integer.class)) {
						field.set(obj, jsonObject.getIntValue(camelFieldName));
					} else if (typeName.equals(Double.class)) {
						field.set(obj, jsonObject.getDoubleValue(camelFieldName));
					} else if (typeName.equals(String.class)) {
						field.set(obj, jsonObject.getString(camelFieldName));
					} else if (typeName.equals(Short.class)) {
						field.set(obj, jsonObject.getShortValue(camelFieldName));
					} else if (typeName.equals(Float.class)) {
						field.set(obj, jsonObject.getFloatValue(camelFieldName));
					} else if (typeName.equals(Double.class)) {
						field.set(obj, jsonObject.getDoubleValue(camelFieldName));
					} else {
						field.set(obj, null);
					}
				}

				// 判断是否存在此下划线属性
				if (jsonObject.containsKey(underscoreFieldName)) {

					// 判断类型并调用set方法
					if (typeName.equals(Integer.class)) {
						field.set(obj, jsonObject.getIntValue(underscoreFieldName));
					} else if (typeName.equals(Double.class)) {
						field.set(obj, jsonObject.getDoubleValue(underscoreFieldName));
					} else if (typeName.equals(String.class)) {
						field.set(obj, jsonObject.getString(underscoreFieldName));
					} else if (typeName.equals(Short.class)) {
						field.set(obj, jsonObject.getShortValue(underscoreFieldName));
					} else if (typeName.equals(Float.class)) {
						field.set(obj, jsonObject.getFloatValue(underscoreFieldName));
					} else if (typeName.equals(Double.class)) {
						field.set(obj, jsonObject.getDoubleValue(underscoreFieldName));
					} else {
						field.set(obj, null);
					}
				}
			}
			// 返回封装对象
			return (T) obj;
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 正确结果
	 * 
	 * @return
	 */
	public final static JSONObject getSuccessJson() {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", Response.SUCCESS.getCode());
		jsonObject.put("msg", Response.SUCCESS.getMsg());
		return jsonObject;
	}

	/**
	 * 错误结果
	 * 
	 * @param response
	 * @return
	 */
	public final static JSONObject getFailJson(Response response) {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", response.getCode());
		jsonObject.put("msg", response.getMsg());
		return jsonObject;
	}
	public static void main(String[] args) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("userId", "123");
		jsonObject.put("headPath", "1111");
		
		
		
		UserInfo info = JsonToObjectOfClass(jsonObject, UserInfo.class);
		System.out.println(info);
		System.out.println(JSON.toJSONString(info));
	}
}
